package com.cloud.force.interfaces;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cloud.force.api.bean.HumanDetail;
import com.cloud.force.common.config.FeignConfiguration;
import com.cloud.force.common.result.SelectItem;

import feign.hystrix.FallbackFactory;

@FeignClient(value = "SYSTEM",  configuration = FeignConfiguration.class, fallbackFactory = FeignClientSystemFallbackFactory.class)
public interface FeignClientSystem {
	/**
	 * 获取字典项
	 * @param sysId 子系统编码
	 * @param dictKey 字典键值
	 * @return
	 */
	@RequestMapping(value = "system/interface/sysdict/listdicts", method = RequestMethod.GET)
	public List<SelectItem> loadDictItems(@RequestParam("sysId") Long sysId, @RequestParam("dictKey") String dictKey);

	/**
	 * 获取当前登录人SQL解析字段相关信息
	 * @param humanId
	 * @return
	 */
	@RequestMapping(value = "system/interface/humanunit/info", method = RequestMethod.GET)
	public Map<String, String> getSystemParams(@RequestParam("humanId")long humanId);
	
	/**
	 * 获取当前登录机构用户基本信息
	 */
	@RequestMapping(value = "system/interface/humanunit/detail", method = RequestMethod.GET)
	public HumanDetail getHumanDetail(@RequestParam("humanId")long humanId);
	
	/**
	 * 获取当前登录机构领域信息
	 * @param unitId
	 * @return
	 */
	@RequestMapping(value = "system/interface/domains/load", method = RequestMethod.GET)
	public List<SelectItem> loadUnitDomains(@RequestParam("unitId")long unitId);

	/**
	 * 查找指定机构下的执法队员信息
	 * @param unitId
	 * @return
	 */
	@RequestMapping(value = "system/interface/human/searchlawyer", method = RequestMethod.GET)
	public List<HumanDetail> searchLawyerByUnit(@RequestParam("unitId")long unitId, @RequestParam("query")String query);

	@RequestMapping(value = "system/interface/domains/loadUnitDomainsByUnitId", method = RequestMethod.GET)
	List<SelectItem> loadUnitDomainsByUnitId(@RequestParam("unitId")long unitId);

	@RequestMapping(value = "system/interface/sysconfig/item", method = RequestMethod.GET)
	String getMediaRootPath(@RequestParam("sysId") long sysId, @RequestParam("configKey") String configName);

	/**
	 * 查询当前登陆者的所属主体领域
	 * @param unitId 机构
	 * @return
	 */
	@RequestMapping(value = "system/interface/unitInfo/findSubjectDomain", method = RequestMethod.GET)
	List<SelectItem> findSubjectDomain(@RequestParam("unitId")long unitId);
}

@Component
class FeignClientSystemFallbackFactory implements FallbackFactory<FeignClientSystem> {
	private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientSystemFallbackFactory.class);
	@Override
	public FeignClientSystem create(final Throwable cause) {
		return new FeignClientSystem() {
			
			@Override
			public List<SelectItem> loadDictItems(Long sysId, String dictKey) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
				}
				return Collections.emptyList();
			}

			@SuppressWarnings("unchecked")
			@Override
			public Map<String, String> getSystemParams(long humanId) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
				}
				return MapUtils.EMPTY_MAP;
			}

			@Override
			public HumanDetail getHumanDetail(long humanId) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
				}
				return null;
			}

			@Override
			public List<SelectItem> loadUnitDomains(long unitId) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("loadDictItems fallback; reason was:" + cause);
				}
				List<SelectItem> domains = new ArrayList<SelectItem>();
				domains.add(new SelectItem("01", "发展改革"));
				domains.add(new SelectItem("02", "教育"));
				domains.add(new SelectItem("03", "科技"));
				domains.add(new SelectItem("04", "工业和信息化"));
				domains.add(new SelectItem("05", "民委"));
				domains.add(new SelectItem("06", "公安"));
				domains.add(new SelectItem("07", "安全"));
				domains.add(new SelectItem("08", "民政"));
				domains.add(new SelectItem("09", "司法"));
				domains.add(new SelectItem("10", "财政"));
				return domains;
			}

			@Override
			public List<HumanDetail> searchLawyerByUnit(long unitId, String query) {
				if (FeignClientSystemFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientSystemFallbackFactory.LOGGER.debug("searchLawyerByUnit fallback; reason was:" + cause);
				}
				return Collections.emptyList();
			}

			@Override
			public List<SelectItem> loadUnitDomainsByUnitId(long unitId) {
				return null;
			}

			@Override
			public String getMediaRootPath(long sysId, String configName) {
				return null;
			}

			@Override
			public List<SelectItem> findSubjectDomain(long unitId) {
				return null;
			}
		};
	}

}
